const app = getApp(); 
const config = require('../../../config.js');
Page({
  data: {
    pageSize :10
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.data.cardId = options.cardId;
    this.data.kpstart = getDate(-7);
    this.data.kpend = getDate();
    this.data.ffstart = getDate(-7);
    this.data.ffend = getDate();
    this.setData({ kpstart: this.data.kpstart, kpend: this.data.kpend, ffstart: this.data.ffstart, ffend: this.data.ffend, cardId: options.cardId});
    getList(this);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageSize = 10;
    getList(this);
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageSize = this.data.pageSize + 10;
    getList(this);
  },
  //  点击时间组件确定事件  
  bindTimeChange: function (e) {
    // console.log("谁哦按")
    this.setData({
      times: e.detail.value
    })
  },

  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    let temptype = e.target.dataset.t;
    let temidate = e.detail.value;
    if ("ks" == temptype){
      this.data.kpstart = temidate;
    } else if ("ke" == temptype){
      this.data.kpend = temidate;
    } else if ("fs" == temptype) {
      this.data.ffstart = temidate;
    } else if ("fe" == temptype) {
      this.data.ffend = temidate;
    }
    this.setData(this.data);
    getList(this)
  }
});

/**
 * 获取数据
 */
function getList(_this) {
  // console.log(_this.data);
  searchList(_this,function(msg){
    //console.log(msg.data.content.list);
    if (Object.prototype.toString.call(msg.data.content.list) === "[object Array]" && msg.data.content.list.length > 0){
      for (let i = 0; i < msg.data.content.list.length ; i++){
        msg.data.content.list[i]["bankAccount4"] = (undefined != msg.data.content.list[i].bankAccount && msg.data.content.list[i].bankAccount.length > 4) ? msg.data.content.list[i].bankAccount.substr(msg.data.content.list[i].bankAccount.length - 4) : msg.data.content.list[i].bankAccount;
      }
      _this.setData({
        wuliu: msg.data.content.list,
        showView: true,
        cardId: _this.data.cardId
      });
    }else{
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../../images/noData.png"
      });
    }
    
    
  });
}

/**
 * 请求数据
 */

function searchList(_this,callback){
  if (_this.data.kpstart > _this.data.kpend) {
    app.tip("开始时间不能晚于结束时间", "none");
    return false;
  }
  wx.showLoading({ mask: true, title: "加载中" });
  let par = {
    currentPage: 1,
    pageSize: _this.data.pageSize,
    vipNo: _this.data.cardId,
    code: config.tenantCode,
    orderStartDate: _this.data.kpstart,
    orderEndDate: _this.data.kpend,
    exposureStartDate: _this.data.ffstart,
    exposureEndDate: _this.data.ffend
  };
  // console.log(par);
  wx.request({
    url: config.requestUrl + "vipCard/getVipCardDetailsBSOrF6",
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"//;charset=utf-8
    },
    data: par,
    success: function (msg) {
      callback(msg);
      wx.hideLoading();
    },
    fail: function (msg) {
      // console.log(msg);
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../../images/noData.png"
      });
      wx.hideLoading();
    }
  });
}

/**
 * 获取当前日期
 */
var getDate = function (num) {
  let tempDate = new Date();
  if (num != undefined && "" != num) {
    tempDate.setDate(tempDate.getDate() + num);//获取AddDayCount天后的日期 
  }
  let nowDate = new Date(Date.parse(tempDate));
  //年  
  let Y = nowDate.getFullYear();
  //月  
  let M = (nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1);
  //日  
  let D = nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate();
  return Y + "-" + M + "-" + D;
};
